package com.slkj.shilixiaoyuanapp.ui.user.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.github.mikephil.charting.formatter.IFillFormatter;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.base.ActivityInfo;
import com.slkj.shilixiaoyuanapp.base.BaseActivity;
import com.slkj.shilixiaoyuanapp.net.HttpHeper;
import com.slkj.shilixiaoyuanapp.net.callback.CommonCallBack;
import com.slkj.shilixiaoyuanapp.util.TestDataProvider;
import com.slkj.shilixiaoyuanapp.util.TxtUtil;
import com.slkj.shilixiaoyuanapp.view.dialog.LoadSuccessAndFailDialog;

import org.reactivestreams.Subscription;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

@ActivityInfo( layout = R.layout.bang_ding_shou_ji_hao,title = "手机号绑定")
public class BangDingShouJiHaoActivity extends BaseActivity {
    @BindView(R.id.et_phonenum)
    EditText PhoneNum;
    @BindView(R.id.et_phoneCode)
    EditText PhoneCode;
    @BindView(R.id.tv_get_phonecode)
    TextView getPhoneCode;
    private Disposable mdDisposable;

    @Override
    protected void init() {

    }

    @OnClick(R.id.tv_get_phonecode) void getCode(){
        String phone = PhoneNum.getText().toString();
        if (TextUtils.isEmpty(phone)){
            showToast("请输入手机号");
            return;
        }
        HttpHeper.get().userService().getCode(phone)
                .compose(getThread())
                .compose(bindToLifecycle())
                .subscribe(new CommonCallBack<String>(true,this) {
                    @Override
                    public void onCallBackSuccess(String data) {
                        LoadSuccessAndFailDialog.showSuccess(BangDingShouJiHaoActivity.this,data);
                        mdDisposable = Flowable.intervalRange(0, 60, 0, 1, TimeUnit.SECONDS)
                                .observeOn(AndroidSchedulers.mainThread())
                                .doOnNext(aLong -> {
                                    getPhoneCode.setText((60 -aLong)+"秒");
                                })
                                .doOnComplete(() -> {
                                    getPhoneCode.setEnabled(true);
                                    getPhoneCode.setText("获取验证码");
                                })
                                .doOnSubscribe(subscription -> getPhoneCode.setEnabled(false))
                                .subscribe();
                    }
                });
    }

    @OnClick(R.id.tv_sure) void sure(){
        String phone = PhoneNum.getText().toString();
        if (TextUtils.isEmpty(phone)){
            showToast("请输入手机号");
            return;
        }
        String code = PhoneCode.getText().toString();
        if (TextUtils.isEmpty(code)){
            showToast("请输入验证码");
            return;
        }
        HttpHeper.get().userService().updatePhone(code,phone)
                .compose(getThread())
                .compose(bindToLifecycle())
                .subscribe(new CommonCallBack<String>(true,this) {
                    @Override
                    public void onCallBackSuccess(String data) {
                        LoadSuccessAndFailDialog.showSuccess(BangDingShouJiHaoActivity.this,data);
                        finish();
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mdDisposable != null && !mdDisposable.isDisposed()){
            mdDisposable.dispose();
        }
    }
}
